package servlet;

import Dao.StudentDao;
import Impl.StudentDaoImpl;
import com.google.gson.Gson;
import model.Result;
import model.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet("/UpdateStudentServlet")
public class UpdateStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

        //1.获取信息
        String stuNo=request.getParameter("stuNo");
        String stuName=request.getParameter("stuName");
        String classes=request.getParameter("classes");
        String department=request.getParameter("department");
        String dormno=request.getParameter("dormno");
        String gender=request.getParameter("gender");
        String tel=request.getParameter("tel");

        //后端判别合法性要做吗
        //？

        Student student=new Student(stuNo,stuName,classes,gender,department,tel,dormno,"");

        StudentDaoImpl studentDao = new StudentDao();
        int resutl=studentDao.updateStudent(student);

        PrintWriter writer = response.getWriter();
        Gson gson=new Gson();

        if(resutl==1)
            writer.write(gson.toJson(new Result("ok")));
        else
            writer.write(gson.toJson(new Result("false")));

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
